﻿
namespace SistemaLogistica.DataAccess
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using SistemaLogistica.Business.Entities;

    public class FormularioData : BaseDataAccess
    {
        private FormularioData() { }

        private readonly static FormularioData instancia = new FormularioData();

        public static FormularioData Instancia
        {
            get { return instancia; }
        }

        public List<Formulario> ListarPorMenu(byte menuID)
        {
            List<Formulario> formularios = new List<Formulario>();

            DbCommand command = db.GetStoredProcCommand("GetFormularioByIdMenu");
            db.AddInParameter(command, "idMenu", DbType.Byte, menuID);

            using (IDataReader dr = db.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    formularios.Add(new Formulario
                    {
                        ClassName = dr.GetString(dr.GetOrdinal("ClassName")),
                        Descripcion = dr.GetString(dr.GetOrdinal("Descripcion")),
                        Estado = dr.GetBoolean(dr.GetOrdinal("Estado")),
                        IdFormulario = dr.GetByte(dr.GetOrdinal("IdFormulario")),
                        IdMenu = dr.GetByte(dr.GetOrdinal("IdMenu")),
                        Nombre = dr.GetString(dr.GetOrdinal("Nombre"))
                    });
                }
            }
            return formularios;
        }

        public Formulario ObtenerPorID(byte formularioID)
        {
            Formulario formulario = null;

            DbCommand command = db.GetStoredProcCommand("GetFormularioByIdFormulario");
            db.AddInParameter(command, "idFormulario", DbType.Byte, formularioID);

            using (IDataReader dr = db.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    formulario = new Formulario
                    {
                        ClassName = dr.GetString(dr.GetOrdinal("ClassName")),
                        Descripcion = dr.GetString(dr.GetOrdinal("Descripcion")),
                        Estado = dr.GetBoolean(dr.GetOrdinal("Estado")),
                        IdFormulario = dr.GetByte(dr.GetOrdinal("IdFormulario")),
                        IdMenu = dr.GetByte(dr.GetOrdinal("IdMenu")),
                        Nombre = dr.GetString(dr.GetOrdinal("Nombre")),
                        Menus = MenusData.Instancia.ObtenerPorID(dr.GetByte(dr.GetOrdinal("IdMenu")))
                    };
                }
            }
            return formulario;
        }
    }
}
